use crate::runtime::error::MagicBoxError;
use crate::runtime::response::{Code, MagicBoxResponse};
use crate::runtime::result::MagicBoxResult;
use crate::utils::request;

#[tauri::command(rename_all = "snake_case")]
pub(crate) async fn invoke_get_http_content(url: String) -> MagicBoxResult<MagicBoxResponse<String>> {
    let response = request::http_get_bytes(url.as_str(), None).await?;

    let content = String::from_utf8(response);
    if content.is_err() {
        let err = content.err().unwrap().to_string();
        log::error!("获取网络内容失败: {}", err.as_str());

        return Err(MagicBoxError::new(Code::NetworkError, err.as_str()));
    }

    Ok(MagicBoxResponse::new_success(content.unwrap()))
}